package com.zhongkun.datahome.service.res.assets;

import com.baomidou.mybatisplus.annotation.TableField;
import com.zhongkun.datahome.service.constant.DataAssetsShowScopeEnum;
import com.zhongkun.datahome.service.constant.DataAssetsTypeEnum;
import com.zhongkun.datahome.service.domain.CommonFile;
import com.zhongkun.datahome.service.domain.DataAssets;
import com.zhongkun.datahome.service.res.CommonFileVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/18 13:56
 * @Desc
 */
@Data
public class DataAssetsVo extends DataAssets {
    @TableField(exist = false)
    @ApiModelProperty("分组类型名称")
    private String groupTypeName;

    @TableField(exist = false)
    @ApiModelProperty("操作人")
    private String operatorName;

    @TableField(exist = false)
    @ApiModelProperty("文件列表")
    private List<CommonFileVo> fileVoList;

    @ApiModelProperty("可见房产列表")
    private List<DataAssetsShowHouseVo> showHouseList;

    public String getAssetsTypeDesc() {
        return DataAssetsTypeEnum.getDesc(this.getAssetsType());
    }

    public String getShowRoomNumList() {
        if (DataAssetsShowScopeEnum.NO_SHOW.is(this.getShowScope())) {
            return "不展示";
        }
        if (DataAssetsShowScopeEnum.ALL_SHOW.is(this.getShowScope())) {
            return "全部业主";
        }
        if (CollectionUtils.isEmpty(this.getShowHouseList())) {
            return "";
        }

        return this.getShowHouseList().stream()
                .map(DataAssetsShowHouseVo::getRoomNum)
                .collect(Collectors.joining(","));
    }

    public static DataAssetsVo convert(DataAssets dataAssets, Map<String, List<CommonFile>> fileGroupMap) {
        DataAssetsVo dataAssetsVo = new DataAssetsVo();
        dataAssetsVo.update(dataAssets, fileGroupMap.getOrDefault(dataAssets.getId().toString(), Collections.emptyList()));
        return dataAssetsVo;
    }

    public void update(DataAssets dataAssets, List<CommonFile> commonFileList) {
        this.setId(dataAssets.getId());
        this.setShowScope(dataAssets.getShowScope());
        this.setAssetsType(dataAssets.getAssetsType());
        this.setAssetsTitle(dataAssets.getAssetsTitle());
        this.setAssetsText(dataAssets.getAssetsText());
        this.setAssetsStatus(dataAssets.getAssetsStatus());
        this.setCreateBy(dataAssets.getCreateBy());
        this.setCreateTime(dataAssets.getCreateTime());
        this.setUpdateBy(dataAssets.getUpdateBy());
        this.setUpdateTime(dataAssets.getUpdateTime());

        List<CommonFileVo> commonFileVos = commonFileList.stream()
                .map(CommonFileVo::convert)
                .sorted(Comparator.comparing(CommonFileVo::getId).reversed())
                .collect(Collectors.toList());
        this.setFileVoList(commonFileVos);
    }
}
